package com.scau.action;

import java.util.List;

import net.sf.json.JSONArray;

import com.scau.service.ISaleOrderService;
import com.scau.service.IStoreInfoService;
import com.scau.vo.SaleOrder;

/**
 * @author Jinglun
 * @create 2017年4月22日
 */
public class SaleOrderAction extends SuperAction {
	private String result;// 属性result，用于异步传输数据交互
	private String year; // 前端年份数值传值到后台
	
	private String start;
	private String end; // 开始与结束时间
	
	private ISaleOrderService saleOrderService;
	private SaleOrder saleOrder;
	
	public String saveSaleOrder(){
		SaleOrder saleO = getSaleOrder();
		//System.out.println("123:"+saleO.getAdmin().getId());
		
		if(saleOrderService.saveSaleOrder(saleO)){
			List<SaleOrder> soList = saleOrderService.listSaleOrder();
			JSONArray jsonArray = JSONArray.fromObject(soList.toString());
			result = jsonArray.toString();
			return "success";
		} else return "error";
	}
	
	public String listSaleOrder(){
		List<SaleOrder> soList = saleOrderService.listSaleOrder();
		JSONArray jsonArray = JSONArray.fromObject(soList.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	// 查出所有挂账
	public String listDebts(){
		List<SaleOrder> debtsList = saleOrderService.listDebts();
		JSONArray jsonArray = JSONArray.fromObject(debtsList.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	// 根据订单号查出一条订单
	public String getOneSaleOrder(){
		SaleOrder so = getSaleOrder();
		System.out.println("查询的订单号："+so.getSaleOrderNo());
		List<SaleOrder> resultSaleOrder = saleOrderService.getOneSaleOrder(so.getSaleOrderNo());
		
		JSONArray jsonArray = JSONArray.fromObject(resultSaleOrder.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	// 改变订单的状态
	public String updateSO(){
		SaleOrder so = getSaleOrder();
		
		if(saleOrderService.updateSO(so.getSaleOrderNo())){
			List<SaleOrder> debtsList = saleOrderService.listDebts();
			JSONArray jsonArray = JSONArray.fromObject(debtsList.toString());
			result = jsonArray.toString();
			return "success";
		} else return "error";
				
	}
	
	// 统计每个月的总销售额
	public String eachMonthSales(){
		year = getYear(); // 获取要查的年份
		
		List<String> resultSales = saleOrderService.eachMonthSales(year);
		JSONArray jsonArray = JSONArray.fromObject(resultSales.toString());
		result = jsonArray.toString();
	
		return "success";
	}
	
	// 根据前端传来的开始与结束时间查询
	public String periodSales(){
		start = getStart();
		end = getEnd();
		
		List resultList = saleOrderService.periodSales(start, end);
		JSONArray jsonArray = JSONArray.fromObject(resultList);
		result = jsonArray.toString();
		
		return "success";
	}
	
	
	// get,set methods
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
	public ISaleOrderService getSaleOrderService() {
		return saleOrderService;
	}


	public void setSaleOrderService(ISaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}


	public SaleOrder getSaleOrder() {
		return saleOrder;
	}
	
	public void setSaleOrder(SaleOrder saleOrder) {
		this.saleOrder = saleOrder;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}
	
	
}
